package com.oktest.measure.ui.adapter

import android.graphics.drawable.Drawable
import android.view.View
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.DraggableModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.oktest.measure.R
import com.oktest.measure.entity.WorkHistoryEntity
import com.oktest.measure.kt.TASK_COMPLETED
import kotlinx.android.synthetic.main.item_work_history.view.*

/**
 * @Author hhh
 * @Date 2020/9/20
 */
class HistoryTestSwipeAdapter() :
    BaseQuickAdapter<WorkHistoryEntity, BaseViewHolder>(
        R.layout.item_work_history), DraggableModule {
    override fun convert(holder: BaseViewHolder, item: WorkHistoryEntity) {
        holder.itemView.apply {
            ivState.setImageDrawable(getStateDrawable(item.taskState))
            tvGroupName.text = item.taskName
            tvTime.text = item.time
            tvGroupTaskState.text = item.taskState
            tvGroupTaskState.setTextColor(ContextCompat.getColor(context,stateTextColor(item.taskState)))
            tvTaskNum.setRightText(item.taskNum.toString())
            tvTaskDone.setRightText(item.overNum.toString())
            tag = isSwipeEnable(item.taskState)
        }
    }

    private fun getDoneVisible(taskState: String): Int {
        return if (taskState == TASK_COMPLETED) {
            View.INVISIBLE
        } else {
            View.VISIBLE
        }
    }

    private fun isSwipeEnable(taskState: String) = taskState != TASK_COMPLETED

    private fun getStateDrawable(state: String): Drawable?{
        return if (state == TASK_COMPLETED) {
            ContextCompat.getDrawable(context,R.drawable.ic_work_history_item_over)
        } else {
            ContextCompat.getDrawable(context,R.drawable.ic_work_history_item_more)
        }
    }

    private fun stateTextColor(state: String) = if (state == TASK_COMPLETED)  R.color.green else R.color.red
}